package com.yfux.cling_player.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.MediaStore;

import java.util.List;

public class WifiUtil {
    // Device Wifi Information
    private static final String UNKNOWN = "<unknown>";
    private static final String WIFI_DISABLED = "<disabled>";
    private static final String WIFI_NO_CONNECT = "<not connect>";
    private static final String PERMISSION_DENIED = "<permission denied>";

    public static String getWiFiIpAddress(Context context) {
        int ipAddress = 0;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        } else {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                Network network = connectivityManager.getActiveNetwork();
                if (network != null) {
                    WifiInfo wifiInfo = (WifiInfo) connectivityManager.getNetworkCapabilities(network).getTransportInfo();
                    if (wifiInfo != null) {
                        ipAddress = wifiInfo.getIpAddress();
                    }
                }
            }
        }
        if (ipAddress == 0) return "";
        return (ipAddress & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "." + ((ipAddress >> 16) & 0xFF) + "." + ((ipAddress >> 24) & 0xFF);
    }

    public static String getWiFiName(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) return WIFI_DISABLED;
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo == null) return WIFI_NO_CONNECT;

        String ssid = wifiInfo.getSSID();
        if (WifiManager.UNKNOWN_SSID.equals(ssid)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
                    if (configuredNetworks != null) {
                        for (WifiConfiguration config : configuredNetworks) {
                            if (config.networkId == wifiInfo.getNetworkId()) {
                                return config.SSID.replace("\"", "");
                            }
                        }
                    }
                } else {
                    return PERMISSION_DENIED;
                }
            } else {
                return WIFI_NO_CONNECT;
            }
            return UNKNOWN;
        } else {
            return ssid.replace("\"", "");
        }
    }

    public static String getHttpBaseUrl(Context context, int port) {
        return "http://" + getWiFiIpAddress(context) + ":" + port + "/";
    }

    private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                return cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return null;
    }
}